﻿#include <QWidget>
#include <QDialog>
#include "CDevBeltRealign.h"
#include "CSqlOper.h"
#include "CGlobal.h"
#include "Public.h"
#include "CLogOper.h"
#include "CCorrectDevCfgDlg.h"
#include "CAlarmPoint.h"

CDevBeltRealign::CDevBeltRealign(QString sPointGuid, QString sPointName, bool& bRet)
{
	m_pBeltCorrectCtrMange = nullptr;
	m_pCamerCtrMange = nullptr;
	m_pWorkMng = nullptr;
	m_sPointGuid = sPointGuid;
	m_sPointName = sPointName;
	bRet=init(m_sPointGuid);
}

CDevBeltRealign::~CDevBeltRealign()
{
	if (m_pBeltCorrectCtrMange != nullptr)
	{
		delete m_pBeltCorrectCtrMange;
		m_pBeltCorrectCtrMange = nullptr;
	}

	if (m_pCamerCtrMange != nullptr)
	{
		delete m_pCamerCtrMange;
		m_pCamerCtrMange = nullptr;
	}

	if (m_pWorkMng != nullptr)
	{
		delete m_pWorkMng;
		m_pWorkMng = nullptr;
	}

	if (m_pDevData)
	{
		delete m_pDevData;
		m_pDevData = nullptr;
	}
}

bool CDevBeltRealign::ctrDev(SCtrInfo& stCtrInfo)
{
	if (stCtrInfo.m_nCtrDevType == 1)//摄像头
	{
		return m_pCamerCtrMange->CtrDevice(stCtrInfo);
	}
	else if (stCtrInfo.m_nCtrDevType == 0)//本体PLC
	{
		return m_pBeltCorrectCtrMange->CtrDevice(stCtrInfo);
	}
	return false;
}

bool CDevBeltRealign::init(QString sPointGuid)
{
	m_pDevData = new CDevDataBeltRealign();
	m_pDevData->m_sPointGuid = sPointGuid;
	//首先看看数据库中是否有这个设备的信息
	{
		//数据库中初始化设备数据
		QString sErrMsg = "";
		QVector<QSqlRecord> vecRecord;
		bool bSql = CSqlOper::instance().selectDevInfoName(sPointGuid, vecRecord, sErrMsg);
		if (!bSql)
		{
			return false;
		}
		if (vecRecord.size() == 0)
		{
			CCorrectDevCfgDlg oper(sPointGuid);
			int nRet = oper.exec();
			if (!nRet == QDialog::Accepted)
			{
				return false;
			}
		}
	}
	{
		//数据库中初始化设备数据类
		QString sErrMsg = "";
		bool bRet = CSqlOper::instance().selectCorrectInfo(sPointGuid, m_pDevData, sErrMsg);
		if (!bRet)
		{
			return false;
		}

		//初始化设备测点集合
		CAlarmPoint* pPointPlcConnectState = new CAlarmPoint("PlcConnectState", A2T("PLC连接状态"), NULL, EPointType::enDigital, "");
		pPointPlcConnectState->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enPlcConnectState, pPointPlcConnectState);

		CMeasurePoint* pPointRecRemoteAuto = new CMeasurePoint("RecRemoteAuto",  A2T("远程状态下自动"), "", nullptr, EPointType::enAnalog, "");
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enRecRemoteAuto, pPointRecRemoteAuto);

		CMeasurePoint* pPointRecRemoteHand = new CMeasurePoint("RecRemoteHand",  A2T("远程状态下手动"), "", nullptr, EPointType::enAnalog, "");
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enRecRemoteHand, pPointRecRemoteHand);

		CMeasurePoint* pPointRecOffsetValue = new CMeasurePoint("RecOffsetValue", A2T("偏移量"), "", nullptr, EPointType::enAnalog, "");
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enRecOffsetValue, pPointRecOffsetValue);

		CMeasurePoint* pPointRecCamerLeft = new CMeasurePoint("RecCamerLeft",  A2T("相机左反馈"), "", nullptr, EPointType::enAnalog, "");
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enRecCamerLeft, pPointRecCamerLeft);

		CMeasurePoint* pPointRecCamerRight = new CMeasurePoint("RecCamerRight", A2T("相机右反馈"), "", nullptr, EPointType::enAnalog, "");
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enRecCamerRight, pPointRecCamerRight);

		CMeasurePoint* pPointRecErrorAlarm = new CMeasurePoint("RecErrorAlarm", A2T("设备报警字"), "", nullptr, EPointType::enAnalog, "");
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enRecErrorAlarm, pPointRecErrorAlarm);

		CMeasurePoint* pPointRecRemoteCtlFeedback = new CMeasurePoint("RecRemoteCtlFeedback", A2T("远程控制反馈"), "", nullptr, EPointType::enAnalog, "");
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enRecRemoteCtlFeedback, pPointRecRemoteCtlFeedback);

		CMeasurePoint* pPointRecLocalCtlFeedback = new CMeasurePoint("RecLocalCtlFeedback",  A2T("本地控制反馈"), "", nullptr, EPointType::enAnalog, "");
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enRecLocalCtlFeedback, pPointRecLocalCtlFeedback);

		CMeasurePoint* pPointRecLocalLeftRun = new CMeasurePoint("RecLocalLeftRun", A2T("本地左偏运行"), "", nullptr, EPointType::enAnalog, "");
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enRecLocalLeftRun, pPointRecLocalLeftRun);

		CMeasurePoint* pPointRecLocalRightRun = new CMeasurePoint("RecLocalRightRun",  A2T("本地右偏运行"), "", nullptr, EPointType::enAnalog, "");
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enRecLocalRightRun, pPointRecLocalRightRun);

		CMeasurePoint* pPointRecMotorRunFeedback = new CMeasurePoint("RecMotorRunFeedback",  A2T("电机运行反馈"), "", nullptr, EPointType::enAnalog, "");
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enRecMotorRunFeedback, pPointRecMotorRunFeedback);

		CMeasurePoint* pPointRecLeftYVRunFeedback = new CMeasurePoint("RecLeftYVRunFeedback", A2T("左偏YV运行反馈"), "", nullptr, EPointType::enAnalog, "");
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enRecLeftYVRunFeedback, pPointRecLeftYVRunFeedback);

		CMeasurePoint* pPointRecRightYVRunFeedback = new CMeasurePoint("RecRightYVRunFeedback",  A2T("右偏YV运行反馈"), "", nullptr, EPointType::enAnalog, "");
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enRecRightYVRunFeedback, pPointRecRightYVRunFeedback);

		CMeasurePoint* pPointLeftCurrVal = new CMeasurePoint("LeftCurrVal",  A2T("左侧实时偏离值"), "", nullptr, EPointType::enAnalog, "cm");
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enLeftCurrVal, pPointLeftCurrVal);

		CMeasurePoint* pPointRightCurrVal = new CMeasurePoint("RightCurrVal",  A2T("右侧实时偏离值"), "", nullptr, EPointType::enAnalog, "cm");
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enRightCurrVal, pPointRightCurrVal);

		CAlarmPoint* pPointCamerState = new CAlarmPoint("CamerState", A2T("摄像头在线状态"), NULL, EPointType::enDigital, "");
		pPointCamerState->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enCamerState, pPointCamerState);

		CAlarmPoint* pPointAlgState = new CAlarmPoint("AlgState",A2T("算法在线状态"), NULL, EPointType::enDigital, "");
		pPointAlgState->setAlarmValue(false);
		m_pDevData->m_mapDevPoint.insert(EDevPointType::enAlgState, pPointAlgState);
	}
	//查询设备输入测点
	{
		QString sErrMsg = "";
		CSqlOper::instance().selectTagConfig(m_sPointGuid,m_pDevData, sErrMsg);
	}
	//创建纠偏点的视频控制类、纠偏设备控制类
	m_pCamerCtrMange = new CCamerCtrMange(m_pDevData->m_pDevDataCam);
	if (m_pDevData->m_nIsHaveDev)
	{
		//首先去地址表里边把地址查找到
		QString sErrMsg = "";
		CSqlOper::instance().selectIoAllocationByID(m_pDevData, sErrMsg);//机器人寄存器信息查询并赋值

		//创建跑偏设备控制类
		m_pBeltCorrectCtrMange = new CCorrectCtrMange(m_pDevData);
	}
	//创建跑偏业务逻辑类
	m_pWorkMng = new CWorkMng(m_pCamerCtrMange, m_pDevData);
}
